package cn.liutao.dish.api;

import cn.liutao.common.enums.CommonEnum;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.dish.entity.po.Cuisine;
import cn.liutao.dish.service.CuisineService;
import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author liutao
 * @Description 菜系Api
 * @date 2023/10/31 17:03
 * @Version 1.0
 */
@WebServlet("/cuisine")
@Slf4j
public class CuisineServlet extends HttpServlet {
    private CuisineService cuisineService;

    @Override
    public void init() throws ServletException {
        cuisineService = CuisineService.getInstance();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  {
        Cuisine cuisine = ServletUtils.getQueryParam(req, Cuisine.class);
        if (cuisine==null) cuisine.setStatus(CommonEnum.STATUS_ON.getIntValue());
        
        Page<Cuisine> page  = cuisineService.selectByList(cuisine);
        ServletUtils.ok(resp,page);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        Cuisine cuisine = ServletUtils.getBodyParam(req, Cuisine.class);
        cuisineService.insert(cuisine);
        ServletUtils.ok(resp,"添加成功");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
        Cuisine cuisine = ServletUtils.getBodyParam(req, Cuisine.class);
        cuisineService.updateById(cuisine);
        ServletUtils.ok(resp,"修改成功");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)  {
        List<String> ids = ServletUtils.getQueryParams(req, "id");
        for (String id : ids) {
            cuisineService.deleteById(Integer.valueOf(id));
        }
        ServletUtils.ok(resp,"删除成功");
    }
}
